
package com.distribute.framework.config;

import java.util.Properties;
import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;

//根据规则匹配数据源
@MapperScan(basePackages = "distribute.sys", sqlSessionTemplateRef = "distributeSysSqlSessionTemplate")
@Configuration
public class DistributeSysDataSourceConfig {

	@Primary
    @Bean(name = "distributeSysDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.distribute_sys")    
    public DataSource setDataSource() {
        return DataSourceBuilder.create().build();
    }

	@Primary
    @Bean(name = "distributeSysTransactionManager")    
    public DataSourceTransactionManager getTransactionManager(@Qualifier("distributeSysDataSource") DataSource dataSource) {
    	 return new DataSourceTransactionManager(dataSource);
    }
    
    @Primary
    @Bean(name = "distributeSysTransactionInterceptor")
    public TransactionInterceptor transactionInterceptor(@Qualifier("distributeSysTransactionManager") PlatformTransactionManager platformTransactionManager) {
        TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
        transactionInterceptor.setTransactionManager(platformTransactionManager);
        Properties transactionAttributes = new Properties();
        transactionAttributes.setProperty("select*","PROPAGATION_REQUIRED,readOnly");
		transactionAttributes.setProperty("get*","PROPAGATION_REQUIRED,readOnly");
		transactionAttributes.setProperty("persist*","PROPAGATION_REQUIRED,readOnly");
		transactionAttributes.setProperty("remove*","PROPAGATION_REQUIRED");
		transactionAttributes.setProperty("add*","PROPAGATION_REQUIRED");
		transactionAttributes.setProperty("send*","PROPAGATION_REQUIRED");
		transactionAttributes.setProperty("find*","PROPAGATION_REQUIRED,readOnly");
		transactionAttributes.setProperty("update*","PROPAGATION_NESTED");
		transactionAttributes.setProperty("save*","PROPAGATION_NESTED");
		transactionAttributes.setProperty("delete*","PROPAGATION_NESTED");
		transactionAttributes.setProperty("del*","PROPAGATION_NESTED");
		transactionAttributes.setProperty("execute*","PROPAGATION_NESTED");
        transactionInterceptor.setTransactionAttributes(transactionAttributes);
        return transactionInterceptor;
    }
    

	@Primary
    @Bean(name = "distributeSysSqlSessionFactory")    
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("distributeSysDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/distribute/framework/dao/mapping/*.xml"));
        return bean.getObject();
    }

	@Primary
    @Bean(name = "distributeSysSqlSessionTemplate")    
    public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("distributeSysSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}